/**
 * 
 */
package org.vsg.stock.core.indicator.kline;

import java.util.List;

import org.vsg.stock.core.KLineStock;
import org.vsg.stock.core.kline.DailyKLineStock;

/**
 * @author Bill Vison
 *
 */
public class DefaultStockRunningCycleBuilder extends StockRunningCycleBuilder {
	
	
	private int length;
	
	public DefaultStockRunningCycleBuilder(int length) {
		this.length = length;
	}

	/* (non-Javadoc)
	 * @see org.vsg.stock.core.indicator.kline.StockRunningCycleBuilder#buildRunningCycle()
	 */
	@Override
	void buildup() {
		
		// --- build the kline array ----
		DailyKLineStock[] arrangeKLines = new DailyKLineStock[length];
		
		
		// --- mark the index ---
		long lastMinVol = 2147483647;
		int markMinVolIndex = 0;
		
		double lastMinLow = lastMinVol , lastMinOpen = lastMinVol , lastMinClose = lastMinVol , lastMinHigh = lastMinVol;
		int markMinLowIndex = markMinVolIndex , markMinCloseIndex = markMinVolIndex ,
		markMinHighIndex = markMinVolIndex , markMinOpenIndex = markMinVolIndex;
		
		for (int i = 0 ; i < this.dailyImportedList.size() - length ; i++) {
			
			long currentVol = 0;
			double currentLow = 0;
			double currentClose = 0;
			double currentOpen = 0;
			double currentHigh = 0;
			
			for (int j = 0 ; j < arrangeKLines.length ; j++) {
				DailyKLineStock index = this.dailyImportedList.get(i + j);
				currentVol += index.getVol();
				
				currentLow += index.getLow();
				currentClose += index.getClose();
				currentOpen += index.getOpen();
				currentHigh += index.getHigh();
				
			}
			
			
			if (currentVol < lastMinVol) {
				lastMinVol = currentVol;
				markMinVolIndex = i;
			}
			
			if (currentLow < lastMinLow) {
				lastMinLow = currentLow;
				markMinLowIndex = i;
			}

			if (currentClose < lastMinClose) {
				lastMinClose = currentClose;
				markMinCloseIndex = i;
			}
			
			if (currentOpen < lastMinOpen) {
				lastMinOpen = currentOpen;
				markMinOpenIndex = i;
			}

			if (currentHigh < lastMinHigh) {
				lastMinHigh = currentHigh;
				markMinHighIndex = i;
			}
			
		}

		
		// --- build the cycle ---
		minVolCycle = createMinRunningCycle(markMinVolIndex , KLineStock.Attribute.VOL);
		minLowCycle = createMinRunningCycle(markMinLowIndex , KLineStock.Attribute.LOW);
		minCloseCycle = createMinRunningCycle(markMinCloseIndex , KLineStock.Attribute.CLOSE);

	}
	
	private DefaultRunningCycle minVolCycle;
	
	private DefaultRunningCycle minLowCycle;
	
	private DefaultRunningCycle minOpenCycle;
	
	private DefaultRunningCycle minCloseCycle;
	
	private DefaultRunningCycle minHighCycle;
	
	private DefaultRunningCycle createMinRunningCycle(int index , KLineStock.Attribute stockAttribute) {
		
		DefaultRunningCycle minRunningCycle = new DefaultRunningCycle();
		
		double lastMinValue = 2147483647;
		int markIndex = index;
		
		for (int i = 0 ; i < this.length ; i++) {
			
			DailyKLineStock dailyKLineStock = dailyImportedList.get( index + i );
			
			minRunningCycle.addDailyKLineStockToCycle( dailyImportedList.get( index + i ) );
			
			if (KLineStock.Attribute.OPEN == stockAttribute) {
				
			}
			else if (KLineStock.Attribute.HIGH == stockAttribute) {
				
			}
			else if (KLineStock.Attribute.LOW == stockAttribute) {
				
				if ( dailyKLineStock.getLow()< lastMinValue) {
					lastMinValue = dailyKLineStock.getLow();
					markIndex = index + i;
				}				
			}
			else if (KLineStock.Attribute.CLOSE == stockAttribute) {
				
				if ( dailyKLineStock.getClose() < lastMinValue) {
					lastMinValue = dailyKLineStock.getClose();
					markIndex = index + i;
				}
				
			}
			else if (KLineStock.Attribute.VOL == stockAttribute) {
								
				if ( dailyKLineStock.getVol() < lastMinValue) {
					lastMinValue = dailyKLineStock.getVol();
					markIndex = index + i;
				}
			}			
		}
		
		DailyKLineStock ultimateDailyKLineStock = dailyImportedList.get( markIndex );
		minRunningCycle.setUltimateDate( ultimateDailyKLineStock.getDate() );
		
		return minRunningCycle;
	}
	

	/* (non-Javadoc)
	 * @see org.vsg.stock.core.indicator.kline.StockRunningCycleBuilder#setCycleLength(int)
	 */
	@Override
	void setCycleLength(int length) {

	}

	private List<DailyKLineStock> dailyImportedList;
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.core.indicator.kline.StockRunningCycleBuilder#setDailyKLineStockList(java.util.List)
	 */
	@Override
	void setDailyKLineStockList(List<DailyKLineStock> importedList) {
		dailyImportedList = importedList;
	}

	/* (non-Javadoc)
	 * @see org.vsg.stock.core.indicator.kline.StockRunningCycleBuilder#getMinVolCycle()
	 */
	@Override
	RunningCycle getMinCycle(KLineStock.Attribute stockAttribute) {
		
		if (KLineStock.Attribute.LOW == stockAttribute) {
			return  this.minLowCycle;
		}
		else if (KLineStock.Attribute.CLOSE == stockAttribute) {
			return this.minCloseCycle;
		}
		else if (KLineStock.Attribute.VOL == stockAttribute) {
			return this.minVolCycle;
		}
		return null;
	}

}
